//
//  YTabBarController.m
//  shijianpinggu
//
//  Created by apple on 2016/12/23.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "YTabBarController.h"
#import "YNavigationController.h"
#import "YNetworkViewController.h"
#import "YNewsViewController.h"
#import "YServeViewController.h"
#import "YMineViewController.h"

#define TabbarItemNums 3.0
@interface YTabBarController ()<UITabBarControllerDelegate>

@end

@implementation YTabBarController





- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    
    //self.tabBar.backgroundColor=[UIColor redColor];
    
    
   
    UIView *backView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
    backView.backgroundColor = [UIColor colorWithRed:18/255.0 green:85/255.0 blue:156/255.0 alpha:1];
    [self.tabBar insertSubview:backView atIndex:0];
    self.tabBarController.tabBar.opaque = YES;
    
//    去掉默认返回按钮上的文字
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:0.001],
                                 
                                 NSForegroundColorAttributeName:[UIColor clearColor]};
    
    [item setTitleTextAttributes:attributes forState:UIControlStateNormal];
    
    //添加子控制器
    [self addAllChildVcs];
}
//显示红点
- (void)showBadgeOnItmIndex:(int)index{
    [self removeBadgeOnItemIndex:index];
    //新建小红点
    UIView *bview = [[UIView alloc]init];
    bview.tag = 888+index;
    bview.layer.cornerRadius = 5;
    bview.clipsToBounds = YES;
    bview.backgroundColor = [UIColor redColor];
    CGRect tabFram = self.tabBar.frame;
    
    float percentX = (index+0.6)/TabbarItemNums;
    CGFloat x = ceilf(percentX*tabFram.size.width);
    CGFloat y = ceilf(0.15*tabFram.size.height);
    bview.frame = CGRectMake(x, y, 10, 10);
    [self.tabBar addSubview:bview];
    [self.tabBar bringSubviewToFront:bview];
}
//隐藏红点
-(void)hideBadgeOnItemIndex:(int)index{
    [self removeBadgeOnItemIndex:index];
}
//移除控件
- (void)removeBadgeOnItemIndex:(int)index{
    for (UIView*subView in self.view.subviews) {
        if (subView.tag == 888+index) {
            [subView removeFromSuperview];
        }
    }
}


- (void)addAllChildVcs
{
    YNetworkViewController *networkView = [[YNetworkViewController alloc] init];
    [self addOneChildVc:networkView title:@"首页" imageName:@"bot_shouye" selectedImageName:@"bot_shouye_bb" navigationImage:@"index_01"];
    
   
    
//    YServeViewController *serveView = [YServeViewController new];
//    [self addOneChildVc:serveView title:@"贴心服务" imageName:@"" selectedImageName:@""];
    
    YMineViewController *mineView = [YMineViewController new];
    
    
        if ([UIScreen mainScreen].bounds.size.width > 375) {
            YNewsViewController *toolsView = [YNewsViewController new];
            [self addOneChildVc:toolsView title:@"消息" imageName:@"bot_xiaoxi" selectedImageName:@"bot_xiaoxi_bb" navigationImage:@"wo_topp"];
             [self addOneChildVc:mineView title:@"我" imageName:@"bot_wo" selectedImageName:@"bot_wo_bb" navigationImage:@"wo_topp"];
        }else if ([UIScreen mainScreen].bounds.size.width == 375) {
            YNewsViewController *toolsView = [YNewsViewController new];
            [self addOneChildVc:toolsView title:@"消息" imageName:@"bot_xiaoxi" selectedImageName:@"bot_xiaoxi_bb" navigationImage:@"wo_top"];
            [self addOneChildVc:mineView title:@"我" imageName:@"bot_wo" selectedImageName:@"bot_wo_bb" navigationImage:@"wo_top"];
        }else{
            YNewsViewController *toolsView = [YNewsViewController new];
            [self addOneChildVc:toolsView title:@"消息" imageName:@"bot_xiaoxi" selectedImageName:@"bot_xiaoxi_bb" navigationImage:@"wo_top5"];
            [self addOneChildVc:mineView title:@"我" imageName:@"bot_wo" selectedImageName:@"bot_wo_bb" navigationImage:@"wo_top5"];
        }


    //[self showBadgeOnItmIndex:1];
}


+(void)initialize
{
    //    选中状态颜色
    [[UITabBar appearance] setTintColor:[UIColor colorWithRed:(255)/255.0 green:(197)/255.0 blue:(0)/255.0 alpha:1.0]];
    
    
}
- (void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageN selectedImageName:(NSString *)selectedImageName navigationImage:(NSString *)naImage
{
    childVc.view.backgroundColor = [UIColor whiteColor];
    //改变字体颜色
    [[UITabBarItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:(255)/255.0 green:(255)/255.0 blue:(255)/255.0 alpha:0.5],NSForegroundColorAttributeName,nil]forState:UIControlStateNormal];
    
    [[UITabBarItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]forState:UIControlStateSelected];
    
    
    
    childVc.tabBarItem.title
    = title;
    
    //取消渲染颜色
    UIImage *image = [UIImage imageNamed:imageN];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.image = image;
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = selectedImage;
    YNavigationController *navVc = [[YNavigationController alloc] initWithRootViewController:childVc];
     [navVc.navigationBar setBackgroundImage:[UIImage imageNamed:naImage] forBarMetrics:UIBarMetricsDefault];
    
    [self addChildViewController:navVc];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
